package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.DesignTagDAO;
import com.proactive.webapp.entity.DesignTag;

public class DesignTagDAOImpl implements DesignTagDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void insert(DesignTag designTag) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO design_tag(designID, designTag) VALUES(?,?)";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designTag.getDid());
			smt.setString(2, designTag.getTag());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	public List<DesignTag> getTop(int top, int designCategoryID) {
		// TODO Auto-generated method stub
		List<DesignTag> tagList = new ArrayList<DesignTag>();
		String sql = "SELECT designTag,COUNT(*) AS total FROM design_tag a "
				+ "JOIN design b ON a.designID = b.designID "
				+ "JOIN design_category c ON b.designCategoryID = c.designCategoryID "
				+ "WHERE b.designCategoryID = ? GROUP BY HEX(designTag) "
				+ "ORDER BY total DESC LIMIT ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designCategoryID);
			smt.setInt(2, top);
			rs = smt.executeQuery();
			while(rs.next()){
				DesignTag designTag = new DesignTag();
				designTag.setTag(rs.getString("designTag"));
				tagList.add(designTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}


	@Override
	public List<DesignTag> getListByDesignID(int designID) {
		// TODO Auto-generated method stub
		List<DesignTag> tagList = new ArrayList<DesignTag>();
		String sql = "SELECT * FROM design_tag WHERE designID = ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designID);
			rs = smt.executeQuery();
			while(rs.next()){
				DesignTag designTag = new DesignTag();
				designTag.setTag(rs.getString("designTag"));
				tagList.add(designTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}
	
	@Override
	public void delete(int designID) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM design_tag WHERE designID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designID);
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}
}
